#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

class Student{
private:
    int _id;
    char* _name;
    int _age;
public:
    Student(int id, const char* name, int age)
        :_id(id)
        ,_name(new char[strlen(name) + 1]())
        ,_age(age)
    {
        strcpy(_name, name);
        printf("构造函数\n");
    }

    ~Student(){
        if(_name != nullptr){
            delete[] _name;
            _name = nullptr;
        }
        printf("析构函数\n");
    }

    Student(const Student & rhs)
        :_id(rhs._id)
        ,_name(new char[strlen(rhs._name) + 1]())
        ,_age(rhs._age)
    {
        strcpy(_name, rhs._name);
        printf("拷贝函数\n");
    }

    Student & operator=(const Student &rhs){
        printf("赋值运算\n");
        if(this != &rhs){
            delete [] _name;
            _name = new char[strlen(rhs._name) + 1]();
            strcpy(_name, rhs._name);
            _id = rhs._id;
            _age = rhs._age;
        }
        return *this;
    }

    void print(){
        printf("id: %d, name: %s, age: %d \n", _id, _name, _age);
    }
};

int main()
{
    Student st1 = {0, "zhangsan", 20};
    st1.print();

    Student st2 = {1, "lisi", 21};
    st2.print();

    Student st3 = st1;
    st3.print();

    st2 = st1;
    st2.print();

    return 0;
}

